package zx.x.modules.yshbms.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import zx.x.common.utils.PageUtils;
import zx.x.common.utils.R;
import zx.x.modules.sys.entity.ActivityApplyEntity;
import zx.x.modules.yshapp.entity.GoodsEntity;
import zx.x.modules.yshapp.entity.PicAddressEntity;
import zx.x.modules.yshapp.service.PicAddressService;
import zx.x.modules.yshbms.service.GoodsService;

import java.util.*;


/**
 *
 * @author x
 * @email x
 */
@RestController
@RequestMapping("yshbms/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private PicAddressService picAddressService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = goodsService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
		GoodsEntity goodsEntity = goodsService.getById(id);
        List<PicAddressEntity> lbPicList = picAddressService.list(new QueryWrapper<PicAddressEntity>().eq("goods_id",id));
        List<PicAddressEntity> detailPicList = picAddressService.list(new QueryWrapper<PicAddressEntity>().eq("goods_id",id));
        goodsEntity.setLbPicList(lbPicList);
        goodsEntity.setDetailPicList(detailPicList);
        return R.ok().put("communityEntity", goodsEntity);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @Transactional
    public R save(@RequestBody GoodsEntity goodsEntity){
        Date date = new Date();
        goodsEntity.setCjTime(date);
        goodsEntity.setUpdateTime(date);
        goodsEntity.setDeleteFlag(0);
        goodsService.save(goodsEntity);
        Integer goodsId = goodsEntity.getId();
        if(goodsEntity.getLbPicList()!=null && goodsEntity.getLbPicList().size()>0){
            for (int i = 0; i < goodsEntity.getLbPicList().size(); i++) {
                goodsEntity.getLbPicList().get(i).setGoodsId(goodsId);
                goodsEntity.getLbPicList().get(i).setType(1);
                goodsEntity.getLbPicList().get(i).setCjTime(date);
            }
            picAddressService.saveBatch(goodsEntity.getLbPicList());
        }
        if(goodsEntity.getDetailPicList()!=null && goodsEntity.getDetailPicList().size()>0){
            for (int i = 0; i < goodsEntity.getDetailPicList().size(); i++) {
                goodsEntity.getDetailPicList().get(i).setGoodsId(goodsId);
                goodsEntity.getDetailPicList().get(i).setType(2);
                goodsEntity.getDetailPicList().get(i).setCjTime(date);
            }
            picAddressService.saveBatch(goodsEntity.getDetailPicList());
        }

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody GoodsEntity goodsEntity){
        goodsService.updateById(goodsEntity);
        Integer goodsId = goodsEntity.getId();
        Date date = new Date();
        picAddressService.remove(new QueryWrapper<PicAddressEntity>().eq("goods_id",goodsId));//
        if(goodsEntity.getLbPicList()!=null && goodsEntity.getLbPicList().size()>0){
            for (int i = 0; i < goodsEntity.getLbPicList().size(); i++) {
                goodsEntity.getLbPicList().get(i).setGoodsId(goodsId);
                goodsEntity.getLbPicList().get(i).setType(1);
                goodsEntity.getLbPicList().get(i).setCjTime(date);
            }
            picAddressService.saveBatch(goodsEntity.getLbPicList());
        }
        if(goodsEntity.getDetailPicList()!=null && goodsEntity.getDetailPicList().size()>0){
            for (int i = 0; i < goodsEntity.getDetailPicList().size(); i++) {
                goodsEntity.getDetailPicList().get(i).setGoodsId(goodsId);
                goodsEntity.getDetailPicList().get(i).setType(2);
                goodsEntity.getDetailPicList().get(i).setCjTime(date);
            }
            picAddressService.saveBatch(goodsEntity.getDetailPicList());
        }
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
        List<GoodsEntity> list =new ArrayList<>();
        Date date = new Date();
        for(int i=0;i<ids.length;i++){
            GoodsEntity goods =new GoodsEntity();
            goods.setId(ids[i]);
            goods.setDeleteFlag(1);
            goods.setUpdateTime(date);
            list.add(goods);
        }
        goodsService.updateBatchById(list);
        return R.ok();
    }
    @RequestMapping("/onSale")
    public R onSale(@RequestBody GoodsEntity goodsEntity){
        goodsService.updateById(goodsEntity);
        return R.ok();
    }

}
